use "$out/HS0temp1_bnoSext_2022Sept.dta", clear

merge 1:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 ev w1 wh1 kor ferfi fogvisz1)

* construct EMP (employment) and NONEMP (non-employment) variables *
gen     EMP = 1 if (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (sick_leave_days<6 | sick_leave_days==.) & (fogvisz1==201)
	
codebook anon	
	
drop if HS==1

codebook anon

* flag one observation randomly within each anon *
	set seed 54321
	generate random = uniform()
	by anon (random), sort: gen flag = _n == 1
* gen m for the flagged time period, within each anon
gen m = 0 if flag==1

gen tm = t if flag==1
preserve
	keep if flag==1
	keep anon tm
	rename tm mintm
	sort anon
	tempfile tm
	save `tm'
restore
sort anon
merge m:1 anon using `tm'
drop if _m==2
drop _merge

replace m = t-mintm
drop mintm

gen korm = kor if flag==1
preserve
	keep if flag==1
	keep anon korm
	rename korm minkorm
	sort anon
	tempfile korm
	save `korm'
restore
sort anon
merge m:1 anon using `korm'
drop if _m==2
drop _merge

keep if minkorm>=20 & minkorm<=50
drop minkorm

codebook anon

drop if (m<-24 | m>24) & m!=.

* keep if the year corresponding to m=0 is at least 2009 *
gen yearm = ev if m==0
preserve
	keep if m==0
	keep anon yearm
	rename yearm mintyearm
	sort anon
	tempfile yearm
	save `yearm'
restore
sort anon
merge m:1 anon using `yearm'
drop if _m==2
drop _merge
drop if mintyearm<2009
drop yearm mintyearm

codebook anon

sort anon t
* get the first month of EMP after the shock
egen firstEMPtemp = min(m) if m>0 & EMP==1, by(anon)	
egen firstEMP = max(firstEMPtemp), by(anon)
drop firstEMPtemp

egen EMPm0 = mean(EMP) if m==0, by(anon) 
* keep all anons (and all their observations) for whom either Wml1==1 | Wm0==1
foreach var of varlist EMPm0 {
	egen max`var' = max(`var'), by(anon)
}

keep if (maxEMPm0 == 1)

gen ABS = 1 if firstEMP>=2 & firstEMP<=12
	gen ABSd=firstEMP if ABS==1
	
replace ABS = 0 if firstEMP==1 

* assign random d-s in such a way that the distribution of d-s among controls corresponds to that of the treated
	* 30.44% d=2
	* 27.09% d=3
	* 16.84% d=4
	* 9.21% d=5
	* 5.73% d=6
	* 3.70% d=7
	* 2.13% d=8
	* 1.57% d=9
	* 1.26% d=10
	* 0.93% d=11
	* 1.09% d=12
	
	tab ABS if m==0
	tab ABS if m==0 & (f2.EMP==1 | f3.EMP==1 | f4.EMP==1 | f5.EMP==1 | f6.EMP==1 | f7.EMP==1 | f8.EMP==1 | f9.EMP==1 | f10.EMP==1 | f11.EMP==1 | f12.EMP==1)
	
	tab ABSd if m==0
		
	* create indicators for potential d-s for each ABS=0 anon 
	foreach x of numlist 2/12{
		gen potd`x'=m==0 & f`x'.EMP==1 & ABS==0
	}
	
	* choose d-s:
	
	set seed 12345
	drop random
	gen random = runiform(0,1) if m==0 & ABS==0 & (potd2==1 | potd3==1 | potd4==1 | potd5==1 | potd6==1 | potd7==1 | potd8==1 | potd9==1 | potd10==1 | potd11==1 | potd12==1)
	sum anon if random!=.
	sort random
	
	egen c12=seq() if potd12==1 & ABS==0 & m==0
	gen dtemp=12 if c12>=1 & c12<=5285
	egen c11=seq() if potd11==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=11 if dtemp==. & c11>=1 & c11<=4422
	egen c10=seq() if potd10==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=10 if dtemp==. & c10>=1 & c10<=6049
	egen c9=seq() if potd9==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=9 if dtemp==. & c9>=1 & c9<=7526
	egen c8=seq() if potd8==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=8 if dtemp==. & c8>=1 & c8<=10362
	egen c7=seq() if potd7==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=7 if dtemp==. & c7>=1 & c7<=18292
	egen c6=seq() if potd6==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=6 if dtemp==. & c6>=1 & c6<=28522
	egen c5=seq() if potd5==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=5 if dtemp==. & c5>=1 & c5<=46109
	egen c4=seq() if potd4==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=4 if dtemp==. & c4>=1 & c4<=84746
	egen c3=seq() if potd3==1 & ABS==0 & m==0 & dtemp==.
	replace dtemp=3 if dtemp==. & c3>=1 & c3<=136396
	replace dtemp=2 if dtemp==. & potd2==1 & ABS==0 & m==0
	
	tab dtemp
	sum anon if m==0 & ABS==0 & (potd2==1 | potd3==1 | potd4==1 | potd5==1 | potd6==1 | potd7==1 | potd8==1 | potd9==1 | potd10==1 | potd11==1 | potd12==1) & dtemp==.
	replace ABSd=dtemp if ABSd==.
	tab ABSd if m==0
	
keep if ABSd!=.

keep if m==0
keep anon t ev ABS ABSd

sum
save "$out/potential_controls_updated_tlm_bnoSext2_SL5_d2_12_distrib_in_EMP_2025March25.dta"
